var houseID = parseInt(GetUrlString("id"));

//点击通知我
$("#remind li").on("click",function(){
  var $this = $(this),$dig = $(".coverDig"),type = $(this).attr("data-type");
  if(type==1){
    $("#type").val("1");
    $dig.find(".title").text("降价通知");
    $("#remindDig").find(".gray").text("降价消息会通过短信及手机推送消息通知您");
  }else{
    $("#type").val("2");
    $dig.find(".title").text("开盘通知");
    $("#remindDig").find(".gray").text("开盘消息会通过短信及手机推送消息通知您")
  }
  $("#newBuildingsId").val(houseID);
  $("#remindDig").show().siblings().hide();
  $dig.show();
});
//发送通知我
$("#sendRemind").on("click",function(e){
  e.stopPropagation();
  var isphone = $("#remindMe").find("input[name='mobile']");
  if(checkForm(isphone)==false) return;
  var data = $("#remindMe").serialize() + "&hpWrite_token=" + localStorage.getItem("hpWrite_token");
  $.ajax({
      type:'get',
      url:address+'/notifyPerson/notice.do',
      data:data,
      dataType:'json',
      success:function(result){
        if(result.status ==200){
          $("#remind .hasc .remindPoint").removeClass("bg-gray").addClass("bg-green");
        }
        message(result.message);
      }
  })
  $("#remindDig,.coverDig").hide();
});

// 新房详情
~function getList(houseID){
  var token = localStorage.getItem("hpWrite_token");
  $.ajax({
      type:'GET',
      url:address+'/newBuildings/get.do?format=true',
      data:{
        "id":houseID,
        "hpWrite_token":token
      },
      dataType:'json',
      success:function(result){
        if(result.status ==200){
          var basedata = result.data.newBuildings;
          $("#houseProject").text(basedata.projectDescription);
          $("#layout").text(basedata.layoutDetails);
          window.localStorage.setItem("projectDetails",basedata.projectDetails);
          $("#slide").html(detailSlideTPL(basedata));
          doDetailSlide();
          $("#averagePrice").text(basedata.averagePrice);
          $("#openingQuotation").html(openQTPL(basedata));
          $("#newAndcom").html(newAndcomTPL(basedata,houseID));
          $("#traffic").text(basedata.traffic);
          $("#address").text(basedata.address);
          $("#call").html(callTPL(basedata));
          detailMap(basedata);
        }
      }
  })
}(houseID);


//轮播图
function detailSlideTPL(data){
  var html ='';
  html += '<ul class="slideBox">';
  $.each(data.mainHouseImageList,function(index,info){
    html += '<li><img src="'+info+'&size=640x300"></li>';
  })
    html += '</ul>'+
    '<div class="tips p-lr10 white l-h24 abs b0 l0 w100p bg-white">'+
      '<span class="name">'+data.name+'</span>'+
      '<span class="item white abs r10 t0"><span class="current">1</span><span>&frasl;</span><span class="all">1</span></span>'+
    '</div>';
  return html;
}

//轮播图do
function doDetailSlide(){
  var slength = $('#slide').find('li').length,
      $item = $('#slide').find('.item'),
      current = 1;
  $item.find('.all').text(slength);
  var slider = Swipe(document.getElementById('slide'), {
    auto: 3000,
    callback:function(index) {
      current ++;
      current = current > slength ? 1 : current;
      $item.find('.current').text(current);
    }
  });
};

//户型图
~function houseImg(houseID){
  $.ajax({
      type:'GET',
      url:address+'/houseImage/list.do',
      data:{"newBuildingsId":houseID},
      dataType:'json',
      success:function(result){
        if(result.status ==200){
          var basedata = result.data.list;
          $("#houseImgList").html(houseImgTPL(basedata));
          
          var $li = $("#houseImgList").find("li"),
              w = $("#houseImg").width(),
              l = $li.length;
          $li.width(w/2-15);
          $("#houseImgList").width(w*l/2);
          //户型图滑动
          var myScroll = new IScroll('#houseImg', {
              scrollbars: false,
              mouseWheel: false,
              momentum:false,
              scrollX:true,
              scrollY:false,
              disableMouse:true,
              shrinkScrollbars: 'clip',
          });
        }
      }
  })
}(houseID);
function houseImgTPL(data){
  var html ='';
  $.each(data,function(index,info){
    html += '<li><img class="pic" src="'+info.image+'&size=280x280"></li>';
  })
  html +='</li>';
  return html;
};

//开盘
function openQTPL(data){
  var html = '';
  html += '<p class="m-b5"><span class="black">开盘：</span><span>'+data.openingQuotationDesc+'</span></p>'+
  '<p class="m-tb5">地址：'+data.address+'</p><div class="label orange">';
  var label = data.label.split(";");
  for(var i=0;i<label.length;i++){
    html+='<span>'+label[i]+'</span>';
  }
  html += '</div>';
  return html;
}
//动态、评论
function newAndcomTPL(data,houseID){
  var html ='',star = 0,star2 = 0,evalUser="",content = "",timeStr = "";
  if(data.houseEvaluationStar){
    star = data.houseEvaluationStar
  }
  if(data.houseEvaluation){
    star2 = data.houseEvaluation.star;
    evalUser = data.houseEvaluation.evalUser;
    content = data.houseEvaluation.content;
    timeStr = data.houseEvaluation.timeStr;
  }
  // star = data.houseEvaluationStar,star2=data.houseEvaluation.star;
  html += '<a class="db l-h32 bg-yellow yellow p-lr10 rel" href="newDynamicList.html?id='+houseID+'">最新动态('+data.newDynamicStateTotal+')<i class="icon-more-right dib abs r10 t0 moreR l-h32">&nbsp;</i></a>'+
  '<p class="newDynamic m10 l-h18" id="newDynamic">'+data.newDynamicState+'</p>'+
  '<a class="db l-h32 bg-yellow yellow p-lr10 rel" href="newCommon.html?id='+houseID+'">楼盘点评('+data.houseEvaluationTotal+')<i class="icon-more-right dib abs r10 t0 moreR l-h32">&nbsp;</i></a>'+
  '<div class="p-b10">'+
      '<div class="b-b p10">'+
        '<div class="start fl">';
          for(var s=5;s>0;s--){
            if(s==star){
              html +='<input type="radio" value="'+s+'" class="checked"disabled>';
            }else{
               html +='<input type="radio" value="'+s+'" disabled>';
            }
          }
         
        html+='</div><span class="yellow m-l5">'+star+'</span></div>'+
      '<div class="starList p10 b-b">'+
        '<p class="p-lr10"><div class="start fr">';
        for(var s=5;s>0;s--){
          if(s==star2){
            html +='<input type="radio" value="'+s+'" class="checked" disabled>';
          }else{
             html +='<input type="radio" value="'+s+'" disabled>';
          }
        }
        html+='</div><span>'+evalUser+'</span></p>'+
        '<p class="l-h18 m-t10">'+content+'</p>'+
        '<p class="gray">'+timeStr+'</p>'+
      '</div>'+
      '<span class="b-red red db l-h45 t-c m-t10 f-s16 btnBig" id="clickCommon">点评赚积分，马上点评</span>'+
  '</div>';
  return html;
};
$(document).on("click","#clickCommon",function(){
   var $dig = $(".coverDig");
   $("#commonDig").show().siblings().hide();
   $dig.show();
})
$("#sendCommon").on("click",function(e){
  e.stopPropagation();
  var txtnum = $("#commonForm").find("textarea").val().length;
  if(txtnum <50) {
    message("评论不得少于50字");
    return;
  }
  var data = $("#commonForm").serialize()+'&newBuildingsId='+houseID;
  $.ajax({
      type:'get',
      url:address+'/houseEvaluation/add.do',
      data:data,
      dataType:'json',
      success:function(result){
        message(result.message);
      }
  })
  $(".coverDig").hide();
  $("#commonDig").hide();
})
$("#nouser").on("click",function(e){
    e.stopPropagation();
    if($(this).hasClass("hasChecked")){
      $("#evalUser").show();
      $(this).removeClass("hasChecked");
    }else{
      $("#evalUser").hide();
      $(this).addClass("hasChecked");
    }
})
$(".starAble").find("input").click(function(){
    var val = $(this).val();
    $(".starStr").text(val);
})

// 百度地图
function detailMap(data){
  var map = new BMap.Map("detailMap");
  var point = new BMap.Point(data.longitude,data.latitude);
  map.centerAndZoom(point,16);
  var marker = new BMap.Marker(point);  // 创建标注
  map.addOverlay(marker);              // 将标注添加到地图中
  var label = new BMap.Label(data.name,{offset:new BMap.Size(20,-10)});
  marker.setLabel(label);
};


//房价走势图
houseChart(houseID);
$("#trend").on("click",".titleTab span",function(){
    var type = $(this).attr("data-type");
    $(this).addClass("checked").siblings().removeClass("checked");
    houseChart(houseID,type);
});
function houseChart(houseID,type){
    var type = type ? type : 1;
    $.ajax({
      type:'GET',
      url:address+'/averagePrice/stat.do',
      data:{
        "type":type,
        "newBuildingsId":houseID,
      },
      dataType:'json',
      success:function(result){
        if(result.status ==200){
          var basedata = result.data.list,ym=[],price=[];
          $.each(basedata,function(index,info){
            ym.push(info.ym);
            price.push(info.averagePrice);
          })
          chart(ym,price);
        }
      }
  })
};
// 折线图
function chart(ym,price){
  var data = {
  labels :ym,
  datasets : [
    {
        strokeColor : "rgba(243,129,46,1)",
        pointColor : "rgba(243,129,46,1)",
        pointStrokeColor : "#fff",
        data :price
      }
    ]
  }
  option ={
    pointDotRadius:2,datasetStrokeWidth:1,datasetFill:false
  }
  var ctx = document.getElementById("houseChart").getContext("2d");
  new Chart(ctx).Line(data,option);
};

function callTPL(data){
  var html = '<p>售楼电话</p>'+
  '<a href="tel:'+data.phone+'">'+data.phone+'</a>'+
  '<a href="tel:'+data.phone+'" class="callBtn"></a>';
  return html;
}

$("#trafficMore").on("click",function(){
  var $this = $(this);
  if($this.hasClass("turnDown")){
    $this.removeClass("turnDown");
    $("#traffic").addClass("trafficH");
  }else{
    $this.addClass("turnDown");
    $("#traffic").removeClass("trafficH");
  }
});

$("#layoutMore").on("click",function(){
  var $this = $(this);
  if($this.hasClass("turnDown")){
    $this.removeClass("turnDown");
    $("#layout").addClass("trafficH");
  }else{
    $this.addClass("turnDown");
    $("#layout").removeClass("trafficH");
  }
});

